/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.changeset;

import com.inspur.edp.bff.api.manager.IFSManagerFactory;
import com.inspur.edp.bff.spi.AbstractBffEntityDeserConvertor;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.spi.jsonser.abstractcefchange.AbstractCefDataDeSerializer;
import com.inspur.edp.cef.spi.jsonser.base.StringUtils;
import com.inspur.edp.cef.spi.jsonser.entity.AbstractEntityChangeJsonDeSerializer;
import com.inspur.edp.cef.spi.jsonser.entity.AbstractEntitySerializerItem;
import com.inspur.edp.commonmodel.api.ICMManager;
import com.inspur.edp.commonmodel.spi.AbstractCMEntityChangeJsonDeSerializer;
import io.iec.edp.caf.runtime.config.CefBeanUtil;

import java.lang.reflect.Method;
import java.util.List;

public abstract class AbstractBffChangeJsonDeserializer extends AbstractCMEntityChangeJsonDeSerializer {

    private String fsType;
    private ICMManager manager;
    private IFSManagerFactory fsMgrFactory;

    public AbstractBffChangeJsonDeserializer(String nodeCode, boolean isRoot, String fsType, List<AbstractEntitySerializerItem> serializers) {
        super(nodeCode, isRoot, serializers);
        this.fsType = fsType;
    }

    private IFSManagerFactory getFsMgrFactory() {
        if (fsMgrFactory == null) {
            fsMgrFactory = CefBeanUtil.getAppCtx().getBean(IFSManagerFactory.class);
        }
        return fsMgrFactory;
    }

    @Override
    protected ICMManager getCMManager() {
        if (manager == null)
            manager = getFsMgrFactory().getFSManager(fsType);
        return manager;
    }

    public void setManager(ICMManager manager) {
        this.manager = manager;
    }

    @Override
    protected AbstractCefDataDeSerializer createDataDeSerializer() {
        AbstractBffEntityDeserConvertor deSerializer = (AbstractBffEntityDeserConvertor) this.getCefDataDeSerializer();
        deSerializer.setManager(manager);
        return deSerializer;
    }

    @Override
    protected AbstractEntityChangeJsonDeSerializer buildChildConvertor(String nodeCode) {
        AbstractBffChangeJsonDeserializer childConvertor = (AbstractBffChangeJsonDeserializer) getChildEntityConvertor(nodeCode);
        childConvertor.setManager(manager);
        return childConvertor;
    }

    @Override
    protected String getRealChildCode(String propertyName) {

        Class entityType = getFsMgrFactory().getFSManager(fsType).createData().getClass();
        String transNodeCode = propertyName.substring(0, propertyName.length() - 1);
        if (!propertyName.equals(StringUtils.toCamelCase(propertyName)))
            return transNodeCode;

        for (Method method : entityType.getMethods()) {
            //if (!propertyInfo.GetCustomAttributes(typeof(VMChildAttribute), true).Any())
            //    continue;
            if (method.getReturnType() != IEntityDataCollection.class)
                continue;
            String methodName = method.getName();
            String tansCode = methodName.substring(0, methodName.length() - 1).substring(3);
            if (StringUtils.toCamelCase(tansCode).equals(transNodeCode) || tansCode.equals(transNodeCode))
                return tansCode;
        }
        return propertyName.substring(0, propertyName.length() - 1);
    }
}
